<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="ko">
<head>
	<title>권한관리</title>
	<link rel="stylesheet" type="text/css" href="../css/admin.css" />
	<link rel="stylesheet" type="text/css" href="../css/jquery.treeview.css" />	
	<script src="../js/jquery.js" type="text/javascript"></script>
	<script src="../js/jquery.cookie.js" type="text/javascript"></script>
	<script src="../js/jquery.treeview.js" type="text/javascript"></script>
	<script type="text/javascript">
	    $(function() {
	        $("#tree").treeview({
	            collapsed: true,
	            animated: "medium",
	            control:"#sidetreecontrol",
	            persist: "location"
	        });
	    })
		
	    //체크박스 자동 체크 로직
	    function checkAll(elm)
	    {
	    	var temp = $(elm).attr('id').split('/');
	    	
	    	var type = temp[0];
	    	var depth = temp[1];
	    	var	id = temp[2];
	    	
	    	checkLowItem(type, depth, id);    	
	    	checkHighItem(type, depth, id);    	
	    };	    
    
	    //하위 항목 자동 체크
	  	function checkLowItem(type, depth, id)
	    {
	    	if(depth == 0)
	    	{
				return 1;	    		
	    	}
	    	else
	    	{
				if($("'input:checkbox[id*='"+type +"/"+ depth +"/"+ id+"']'").is(":checked")==false)
				{
					$("'input:checkbox[id*='"+type +"/"+ --depth +"/"+ id+"']'").attr("checked", false);
				}
				else
				{
			    	$("'input:checkbox[id*='"+type +"/"+ --depth +"/"+ id+"']'").attr("checked", true);
				}
				return checkLowItem(type, depth, id);
	    	}
	    }
	    
	    //상위 항목 자동 체크
	    function checkHighItem(type, depth, id){
	    	
	    	var temp = id.split('_');
	    	var name = '';
	    	
	    	for (var i = 0; i < temp.length - 1; i++) {
				name += temp[i] + "_"; 				
			}
			name = name.slice(0, -1);
	    	
	    	if(depth == 4)
	    	{
				return 1;	    		
	    	}
	    	else
	    	{
				if($("'input:checkbox[id*='"+type +"/"+ depth +"/"+ name+"']'").is(":checked")==false)
				{
					$("'input:checkbox[id*='"+type +"/"+ ++depth +"/"+ name+"']'").attr("checked", false);
				}
				else
				{
			    	$("'input:checkbox[id*='"+type +"/"+ ++depth +"/"+ name+"']'").attr("checked", true);
				}
				return checkHighItem(type, depth, name);
	    	}	
	    }
	    
	    //읽기 일괄 선택
	    function checkRead(){
	    	if($("input:checkbox[id*='read']").is(":checked")== false)
	    	{
		    	$("input:checkbox[id*='read']").attr("checked", true);
		    	$("#read_button").val('읽기해제');
	    	}
	    	else
	    	{
		    	$("input:checkbox[id*='read']").attr("checked", false);
		    	$("#read_button").val('읽기선택');
	    	}
	    }

	    //쓰기 일괄 선택
	    function checkWrite(){
	    	if($("input:checkbox[id*='write']").is(":checked")== false)
	    	{
		    	$("input:checkbox[id*='write']").attr("checked", true);
		    	$("#write_button").val('쓰기해제');
	    	}
	    	else
	    	{
		    	$("input:checkbox[id*='write']").attr("checked", false);
		    	$("#write_button").val('쓰기선택');
	    	}
	    }
	    
	    /* 체크 된 아이디 배열에 담은 뒤
	          데이타 AJAX 전달
    	*/
	    function makeMenuJsp()
		{
			var read = new Array();
			var write = new Array();

			$("input:checkbox[id*=read]:checked").each(function(){	
				read.push($(this).val());
			});
			
			$("input:checkbox[id*=write]:checked").each(function(){	
				write.push($(this).val());
			});

			read.push("default");
			write.push("default");
			
			$.ajax({      
		        type:"POST",  
		        url:"/admin/authmng_popup_proc.do",      
		        dataType:'json',
	            data: {read:read, write:write},
		        success:function(args){   
					alert("수정되었습니다.");
					window.close();
		        },   
		        error:function(e){  
		            alert(e.responseText);  
		        }  
		    }); 
		};
	</script>
</head>
<body class="bgno">
	<!-- 본문 영역 -->
	<div class="p20">
		<!-- 본문 내용 -->
			<div>
				<div style="width:70%; float:left;">
					<ul id="tree">
				<c:forEach items="${listMapAuth}" var="listMap1"> 
					<c:if test="${listMap1.MENU_DEPTH eq '1' }">
				       
				        <li>
							<!-- 일기 권한 -->
							<input type="checkbox"  onclick="checkAll(this)" id="read/3/${listMap1.MENU_PATH}"  value="${listMap1.MENU_SID}"
							<c:if test="${listMap1.MENU_CHECK eq 'Y'}">checked="checked"</c:if>/>
							
							<!-- 쓰기 권한 -->
							<input type="checkbox"  onclick="checkAll(this)" id="write/3/${listMap1.MENU_PATH}"  value="${listMap1.MENU_SID}"
							<c:if test="${listMap1.WRITE_AUTH eq 'Y'}">checked="checked"</c:if>/>
				            <strong>${listMap1.MENU_NAME}</strong>
				            <ul>
						<c:forEach items="${listMapAuth}" var="listMap2">
							<c:if test="${ listMap2.PARENT_SID eq listMap1.MENU_SID and listMap2.MENU_DEPTH eq '2' }">
				                
				                <li>
									<!-- 일기 권한 -->
									<input type="checkbox" onclick="checkAll(this)" id="read/2/${listMap2.MENU_PATH}" value="${listMap2.MENU_SID}"
									<c:if test="${listMap2.MENU_CHECK eq 'Y'}">checked="checked"</c:if>/>
									
									<!-- 쓰기 권한 -->
									<input type="checkbox"  onclick="checkAll(this)" id="write/2/${listMap2.MENU_PATH}" value="${listMap2.MENU_SID}"
									<c:if test="${listMap2.WRITE_AUTH eq 'Y'}">checked="checked"</c:if>/>
						            ${listMap2.MENU_NAME}
				                    <ul>
								<c:forEach items="${listMapAuth}"  var="listMap3" >
									<c:if test="${ listMap3.PARENT_SID eq listMap2.MENU_SID and listMap3.MENU_DEPTH eq '3' }">
				                        
				                        <li>
											<!-- 읽기 권한 -->
											<input type="checkbox" onclick="checkAll(this)" id="read/1/${listMap3.MENU_PATH}" value="${listMap3.MENU_SID}"
											<c:if test="${listMap3.MENU_CHECK eq 'Y'}">checked="checked"</c:if>/>
											
											<!-- 쓰기 권한 -->
											<input type="checkbox" onclick="checkAll(this)" id="write/1/${listMap3.MENU_PATH}" value="${listMap3.MENU_SID}"
											<c:if test="${listMap3.WRITE_AUTH eq 'Y'}">checked="checked"</c:if>/>
								            ${listMap3.MENU_NAME}
				                        </li>
					            	</c:if>
					            </c:forEach>
				                    </ul>
				                </li>
			            	</c:if>
			            </c:forEach>
				            </ul>
				        </li>
					</c:if>
				</c:forEach>
				    </ul>
				</div>

				<div>
					<span class="button btnProc01"><a href="#" onclick="makeMenuJsp()">수정하기</a></span>
					<span class="button btnProc01"><input type="button" id="read_button" value="읽기선택" onclick="checkRead()"></span>
					<span class="button btnProc01"><input type="button" id="write_button" value="쓰기선택" onclick="checkWrite()"></span>
				</div>
			</div>
		</div>
	</div>
</body>
</html>